package com.ruoyi.manage.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.manage.domain.ServiceAreaItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.ServiceAreaItemMapper;
import com.ruoyi.manage.domain.ServiceAreaItem;
import com.ruoyi.manage.service.IServiceAreaItemService;

/**
 * 区域服务管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class ServiceAreaItemServiceImpl implements IServiceAreaItemService {
    @Autowired
    private ServiceAreaItemMapper serviceAreaItemMapper;

    /**
     * 查询区域服务管理
     *
     * @param id 区域服务管理主键
     * @return 区域服务管理
     */
    @Override
    public ServiceAreaItem selectServiceAreaItemById(Long id) {
        return serviceAreaItemMapper.selectServiceAreaItemById(id);
    }

    /**
     * 查询区域服务管理列表
     *
     * @param serviceAreaItem 区域服务管理
     * @return 区域服务管理
     */
    @Override
    public List<ServiceAreaItem> selectServiceAreaItemList(ServiceAreaItem serviceAreaItem) {
        return serviceAreaItemMapper.selectServiceAreaItemList(serviceAreaItem);
    }

    /**
     * 新增区域服务管理
     *
     * @param serviceAreaItemVo 区域服务管理
     * @return 结果
     */
    @Override
    public int insertServiceAreaItem(ServiceAreaItemVo serviceAreaItemVo) {
        Long areaId = serviceAreaItemVo.getAreaId();
        ServiceAreaItem item = new ServiceAreaItem();
        item.setServiceAreaId(Math.toIntExact(areaId));
        List<ServiceAreaItem> serviceAreaItems = serviceAreaItemMapper.selectServiceAreaItemList(item);
        List<ServiceAreaItem> collect = serviceAreaItemVo.getServiceItems().stream()
                .filter(items -> serviceAreaItems.stream().noneMatch(serviceAreaItem -> serviceAreaItem.getServiceItmeId().equals(items.getId()))).map(serviceItem -> {
                    ServiceAreaItem serviceAreaItem = new ServiceAreaItem();
                    serviceAreaItem.setServiceAreaId(Math.toIntExact(serviceAreaItemVo.getAreaId()));
                    serviceAreaItem.setServiceItmeId(serviceItem.getId());
                    serviceAreaItem.setServiceTypeId(serviceItem.getServiceTypeId());
                    serviceAreaItem.setServicePrice(serviceItem.getServicePrice());
                    serviceAreaItem.setRegionalPrice(BigDecimal.valueOf(0));
                    serviceAreaItemMapper.insertServiceAreaItem(serviceAreaItem);
                    return serviceAreaItem;
                }).collect(Collectors.toList());


        if (collect.size() > 0) {
            return 1;
        }
        return 0;
    }

    /**
     * 修改区域服务管理
     *
     * @param serviceAreaItem 区域服务管理
     * @return 结果
     */
    @Override
    public int updateServiceAreaItem(ServiceAreaItem serviceAreaItem) {
        return serviceAreaItemMapper.updateServiceAreaItem(serviceAreaItem);
    }

    /**
     * 批量删除区域服务管理
     *
     * @param ids 需要删除的区域服务管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceAreaItemByIds(Long[] ids) {
        return serviceAreaItemMapper.deleteServiceAreaItemByIds(ids);
    }

    /**
     * 删除区域服务管理信息
     *
     * @param id 区域服务管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceAreaItemById(Long id) {
        return serviceAreaItemMapper.deleteServiceAreaItemById(id);
    }

    @Override
    public int updateEnabledState(Long id, Long status) {
        return serviceAreaItemMapper.updateEnabledState(id, status);
    }

    @Override
    public int updateSettingsHot(Long id, Long hot) {
        return serviceAreaItemMapper.updateSettingsHot(id, hot);
    }

    @Override
    public int updateRegionalPrice(Long id, Long regionalPrice) {
        return serviceAreaItemMapper.updateRegionalPrice(id, regionalPrice);
    }
}
